package com.davy.gridpoint.qlxj.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.davy.gridpoint.R;

import java.util.ArrayList;
import java.util.Map;

public class SblxAdapter extends RecyclerView.Adapter<SblxAdapter.ItemViewHolder> {
    private ItemClickListener mItemClick;
    private Context mContext;
    private ArrayList<Map> mList;
    private int pop = -1;

    public SblxAdapter(Context mContext, ArrayList<Map> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    public void setData(ArrayList<Map> vList) {
        this.mList = vList;
        notifyDataSetChanged();
    }

    public void setPop(int pop) {
        this.pop = pop;
        notifyDataSetChanged();
    }

    public void setItemClickListener(ItemClickListener listener) {
        this.mItemClick = listener;
    }

    @Override
    public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemViewHolder itemViewHolder = new ItemViewHolder(LayoutInflater
                .from(mContext).inflate(R.layout.itme_sblx, parent, false));
        return itemViewHolder;
    }

    @Override
    public void onBindViewHolder(ItemViewHolder holder, final int position) {
        if (mList.size() > 0) {
            if (pop == position) {
                holder.radio.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.icon_yd_z));
            } else {
                holder.radio.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.icon_yd_w));
            }
            holder.tvName.setText("" + mList.get(position).get("name"));

            holder.radio.setOnClickListener(v -> {
//                if (StringUtils.isFastClick()) {
//                    return;
//                }
                if (mItemClick != null) {
                    mItemClick.onClick(position, mList.get(position));
                }
            });
            holder.itemView.setOnClickListener(v -> {
//                if (StringUtils.isFastClick()) {
//                    return;
//                }
                if (mItemClick != null) {
                    mItemClick.onClick(position, mList.get(position));
                }
            });
        }
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {
        private TextView tvName;
        private ImageView radio;

        public ItemViewHolder(View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tvName);
            radio = itemView.findViewById(R.id.radio);
        }
    }

    public interface ItemClickListener {
        void onClick(int position, Map map);
    }
}
